﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CustomerCardRelaRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/10/14
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    /// 仓储层
    /// </summary>	
    public class UserCardRelaRepository : BaseRepository<UserCardRela>
    {

        #region 构造

        public UserCardRelaRepository() : base() { }

        public UserCardRelaRepository(string connectionString) : base(connectionString) { }

        public UserCardRelaRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 根据用户查询卡管理
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserCardRela GetByUser(int userId)
        {
            string sql = "SELECT * FROM UserCardRela WITH(NOLOCK) WHERE UserId=" + userId;
            return FirstOrDefault(sql);
        }

        /// <summary>
        /// 获取卡级别
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetCarLevel(int userId)
        {
            string sql = "SELECT T2.Level FROM UserCardRela T WITH(NOLOCK) LEFT JOIN UserCard T2 ON T2.CardId = T.CardId WHERE T.UserId=" + userId;
            var levels = dbContext.Connection.Query<int>(sql).AsList();
            if (levels.Count > 0)
            {
                return levels[0];
            }
            return 0;
        }
    }
}